package com.ugrow.Study_System;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

public class Login extends AppCompatActivity {

    private CheckBox cb;
    EditText username,password;
    Button loginBtn;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        username = (EditText) findViewById(R.id.nameEdit);
        password = (EditText) findViewById(R.id.password1);
        cb = (CheckBox) findViewById(R.id.rememberCheck);
        loginBtn = (Button) findViewById(R.id.btnLogin);
        sp = getSharedPreferences("config", MODE_PRIVATE);

        String spRemeberNameStr = sp.getString("REMEBER_USERNAME","");
        String spRemeberPasswordStr = sp.getString("REMEBER_PASSWORD","");

        String spUserNameStr = sp.getString("USERNAME","");
        String spPasswordStr = sp.getString("PASSWORD","");
        //数据回显
        if(!(spRemeberNameStr.equals(""))){
            //如果REMBER_USERNAME内容不为空,则将用户名密码显示出来
            //如果新注册的用户,则显示空的登录框
            if(TextUtils.equals(spRemeberNameStr,spUserNameStr)&&TextUtils.equals(spPasswordStr,spRemeberPasswordStr)) {
                username.setText(spRemeberNameStr);
                password.setText(spRemeberPasswordStr);
                cb.setChecked(true);
            }
        }
    }

    public void back(View view){    //返回到主界面
        finish();

    }
    public void login(View view) {
        String usernameStr = username.getText().toString().trim();
        String passwordStr = password.getText().toString().trim();


        if(!(TextUtils.isEmpty(usernameStr)||TextUtils.isEmpty(passwordStr))){
            //用户名和密码不为空
            String spUsername;
            String spPassword;
            sp = getSharedPreferences("config", MODE_PRIVATE);
            spUsername = sp.getString("USERNAME", "");
            spPassword = sp.getString("PASSWORD", "");
            if(TextUtils.equals(spUsername,usernameStr)&&TextUtils.equals(spPassword,passwordStr)){
                //判断一下CheckBox的勾选状态
                boolean checked = cb.isChecked();
                if(checked){
                    //存起来
                    sp.edit().putString("REMEBER_USERNAME",usernameStr).putString("REMEBER_PASSWORD",passwordStr).commit();
//                    Toast.makeText(Login.this, "记住密码", Toast.LENGTH_SHORT).show();
                }else{
                    //清空之前存的
                    sp.edit().putString("REMEBER_USERNAME","").putString("REMEBER_PASSWORD","").commit();
                    Toast.makeText(Login.this, "清除成功", Toast.LENGTH_SHORT).show();
                }

                Toast.makeText(Login.this, "登陆成功", Toast.LENGTH_SHORT).show();
            }
            else{
                Toast.makeText(Login.this, "信息不匹配,请重新输入", Toast.LENGTH_SHORT).show();
            }
        }else{
            Toast.makeText(Login.this,"请输入用户名和密码",Toast.LENGTH_SHORT).show();
        }


    }
//    public String test1(String s){
//        return s.matches("")
//    }
}
